﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteInfo_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //txtId.Attributes.Add("readonly", "readonly");
            bd();
        }
    }
    void bd()
    {
        int id =int.Parse(Request["id"]);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.SiteInfo type = dal.SiteInfo.First(x => x.sid == id);
        //txtId.Text = type.sid.ToString();
        Label1.Text = type.sid.ToString();
        txtMemo.Text = type.memo;
        txtName.Text = type.SiteName;
        txtUrl.Text = type.SiteUrl;
        if (type.Weight.Equals((object)null))
        {
            ddlWt.SelectedIndex = 0;
        }
        else
        {
            ddlWt.SelectedValue = type.Weight.ToString();
        }
     
        
        //string id = Request["id"];
        //CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        //ddlPid.DataSource = dal.NewsType.Where(x => x.id != id && string.IsNullOrEmpty(x.pid));
        //ddlPid.DataTextField = "name";
        //ddlPid.DataValueField = "id";
        //ddlPid.DataBind();
        //ddlPid.Items.Insert(0, new ListItem("无", ""));
        //CMSModel.NewsType type = dal.NewsType.First(x => x.id == id);
        //txtId.Text = type.id;
        //txtMemo.Text = type.memo;
        //txtName.Text = type.name;
        //ddlPid.SelectedValue = type.pid;

        //if (type.enable == 1)
        //{
        //    rbtnNormal.Checked = true;
        //}
        //else
        //{
        //    rbtnForbidden.Checked = true;
        //}
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strUrl = string.Empty;
        if (FileUpload1.HasFile)
        {
            string fileType = FileUpload1.PostedFile.ContentType;
            if (fileType == "image/bmp" || fileType == "image/gif" || fileType == "image/pjpeg" || fileType == "image/x-png")
            {
                string saveUrl = @"\images\";
                string appPath = Request.PhysicalApplicationPath;
                string savePath = appPath + saveUrl +
                Server.HtmlEncode(FileUpload1.FileName);
                FileUpload1.SaveAs(savePath);
                strUrl = saveUrl + FileUpload1.FileName; ;
                //ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('文件类型不对！请选择图片！')", true);
                return;
            }
        }
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        int id=int.Parse(Label1.Text);
        CMSModel.SiteInfo type = dal.SiteInfo.First(x => x.sid ==id);
        type.EditTime = DateTime.Now;
        type.sid = int.Parse(Label1.Text);
        type.SiteName = txtName.Text;
        type.memo = txtMemo.Text;
        type.SiteUrl = txtUrl.Text;
        type.Weight = int.Parse(ddlWt.SelectedValue);
        type.pic = strUrl;
        try
        {
            dal.SaveChanges();

            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功'),document.location='Default.aspx'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "EditSiteInfo", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败')", true);
        }

    }
}